package com.cuoyi.coupon.mall.controller;

import com.cuoyi.coupon.mall.common.ApiResult;
import com.cuoyi.coupon.mall.common.Entry;
import com.cuoyi.coupon.mall.manager.ImageManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhang.cheng
 * 2022-02-17 14:32
 **/
@RestController
@Api(value = "图片上传接口", tags = "图片上传接口")
@RequestMapping(value = Entry.ADMIN + "/image")
public class ImageController {

    @Resource
    private ImageManager imageManager;

    @PostMapping(value = "uploadImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "上传图片", notes = "上传图片")
    public ApiResult<String> uploadImage(@RequestParam("file") MultipartFile file) {
        String imageUrl = imageManager.uploadImage(file);
        return ApiResult.ok(imageUrl);
    }

    @GetMapping(value = "getImage", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "读取图片", notes = "读取图片")
    public void getImage(String filename, HttpServletResponse response) {
        if (filename == null || "".equals(filename)) {
            return;
        }
        imageManager.getImage(filename, response);
    }
}
